package com.tanguy.driver.ui.account.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Typeface
import android.widget.EditText
import cn.sinata.xldutils_kotlin.utils.SPUtils
import com.tanguy.driver.App.Companion.app
import com.tanguy.driver.Const
import com.tanguy.driver.R
import com.tanguy.driver.base.ImmersionTitleActivity
import com.tanguy.driver.net.http.HttpManager
import com.tanguy.driver.ui.driver.DriverMainActivity
import com.tanguy.driver.ui.operator.OperatorMainActivity
import com.tanguy.driver.utlis.extend.checkPassword
import com.tanguy.driver.utlis.extend.checkPhoneNumber
import com.tanguy.driver.utlis.extend.loge
import com.tanguy.driver.utlis.extend.requestSucceed
import com.tanguy.user.utlis.extend.click
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult

/**
 * Created by dab on 2017/11/1 0001 10:11
 * 登录
 */
class LoginActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_login
    override fun initView() {
        super.initView()
        titleBar.titleView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        title = "唐吉司机"
        titleBar.showLeft(false)
    }

    override fun initEvent() {
        super.initEvent()
        click(R.id.tv_forget) { startActivityForResult<ForgetPasswordActivity>(Const.RequestCode.FORGET) }
        click(R.id.tv_register) { startActivityForResult<RegisterFirstActivity>(Const.RequestCode.REGISTER) }
        click(R.id.tv_login) {
            val phone = find<EditText>(R.id.et_phone).text.toString().replace(" ", "")
            val password = find<EditText>(R.id.et_password).text.toString().replace(" ", "")
            if (!checkPhoneNumber(phone)) return@click
            if (!checkPassword(password)) return@click
            login(phone,password)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.REGISTER) {
               loge(app.driverRegister.phone+">>>>>>"+ app.driverRegister.password)
            }
            if (requestCode == Const.RequestCode.FORGET) {
               loge("忘记密码")
            }
        }
    }

    private fun login(phone:String,password:String) {
        HttpManager.login(phone, password)
                .requestSucceed(this) {
                    val id = it.get("id").asInt
                    val type = it.get("type").asInt
                    SPUtils.instance().put(Const.User.PHONE,phone).put(Const.User.PASSWORD,password).apply()
                    loge(it)
                    if (type == 1) {
                        //司机
                        startActivity<DriverMainActivity>(Const.User.ID to id)
                        finish()
                    } else {
                        //运营商
                        startActivity<OperatorMainActivity>(Const.User.ID to id)
                        finish()
                    }
                }
    }
}